﻿using Microsoft.Extensions.Configuration;
using System;

namespace RayD.Core
{
    /// <summary>
    /// 配置文件帮助类
    /// </summary>
    public class AppSettings
    {
        private static IConfiguration _config { get; }
        static AppSettings()
        {
            IConfiguration config = null;
            try
            {
                config = ServiceContainer.Current?.GetInstance<IConfiguration>();
            }
            catch
            {

            }
            if (config == null)
            {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appsettings.json", false, true)
                    .Build();
                config = builder;
            }

            _config = config;
        }

        #region 获取配置文件数据
        /// <summary>
        /// 获取项目名
        /// </summary>
        public static ProjectNameSection ProjectName
        {
            get
            {
                return _config
                    .GetSection("ProjectName")
                    .Get<ProjectNameSection>();
            }
        }
        /// <summary>
        /// 默认数据库
        /// </summary>
        public static DbSection DefaultDb
        {
            get
            {
                return _config
                    .GetSection("DefaultDb")
                    .Get<DbSection>();
            }
        }
        /// <summary>
        /// 获取Redis地址
        /// </summary>
        public static RedisSection Redis
        {
            get
            {
                return _config
                    .GetSection("Redis")
                    .Get<RedisSection>();
            }
        }
        /// <summary>
        /// 获取Jwt配置相关
        /// </summary>
        public static JwtSection Jwt
        {
            get
            {
                return _config
                    .GetSection("Jwt")
                    .Get<JwtSection>();
            }
        }
        public static CorsSection Cors
        {
            get
            {
                return _config
                    .GetSection("Cors")
                    .Get<CorsSection>();
            }
        }
        /// <summary>
        /// 获取IM客户端配置相关
        /// </summary>
        public static ImClientOptionSection ImClientOption
        {
            get
            {
                return _config
                    .GetSection("ImClientOption")
                    .Get<ImClientOptionSection>();
            }
        }
        public static MiddlewareSection Middleware
        {
            get
            {
                return _config
                    .GetSection("Middleware")
                    .Get<MiddlewareSection>();
            }
        }
        public static CacheAOPSection CacheAOP
        {
            get
            {
                return _config
                    .GetSection("CacheAOP")
                    .Get<CacheAOPSection>();
            }
        }
        public static MiniProfilerAOPSection MiniProfilerAOP
        {
            get
            {
                return _config
                    .GetSection("MiniProfilerAOP")
                    .Get<MiniProfilerAOPSection>();
            }
        }
        /// <summary>
        /// 获取文件上传绝对路径
        /// </summary>
        public static UploadPathSection UploadPath
        {
            get
            {
                return _config
                    .GetSection("UploadPath")
                    .Get<UploadPathSection>();
            }
        }
        /// <summary>
        /// 获取定时任务配置
        /// </summary>
        public static JobOptionSection JobOption
        {
            get
            {
                return _config
                    .GetSection("JobOption")
                    .Get<JobOptionSection>();
            }
        }
        #endregion

        public class ProjectNameSection
        {
            public string EName { get; set; }
            public string CName { get; set; }
        }
        public class DbSection
        {
            public DataBaseType DataBaseType { get; set; }
            public string ConnectionString { get; set; }
        }
        public class RedisSection
        {
            public string ConnectionString { get; set; }
        }
        public class JwtSection
        {
            public string SigningKey { get; set; }
            public string Issuer { get; set; }
            public string Audience { get; set; }
            public double ExpireTime { get; set; }
        }
        public class CorsSection
        {
            public bool EnableAllIPs { get; set; }
            public string IPs { get; set; }
        }
        public class ImClientOptionSection
        {
            public bool Enabled { get; set; }
            public string RedisClient { get; set; }
            public string Servers { get; set; }
        }
        public class MiddlewareSection
        {
            public RequestResponseLog RequestResponseLog { get; set; }
        }
        public class RequestResponseLog
        {
            public bool Enabled { get; set; }
        }
        public class CacheAOPSection
        {
            public bool Enabled { get; set; }
        }
        public class MiniProfilerAOPSection
        {
            public bool Enabled { get; set; }
        }
        public class UploadPathSection
        {
            public string fileHeadImg { get; set; }
            public string fileEditor { get; set; }
            public string fileAnnexes { get; set; }
            public string fileExcel { get; set; }

        }
        public class JobOptionSection
        {
            public bool Enabled { get; set; }
            public JobTerminal SubjectTerminal { get; set; }
        }
    }
}
